clear
clear matrix
capture log close
version 10
set mem 2g
set more off

* Produce balance plots
* 1. Set parameters
* Matching vars
local vars="gdpC gdpD preA2Bstr_pnl year"
local exact="year"
local exact2="preA2Bstr_pnl"

local k=1

* 1. Matching and ATT for effect of ruling versus panel established

use 02_Rulings.dta
keep `vars' `exact' D1 D3 preA2Bstr_pnl A2B_sflow_p1_pnl A2B_sflow_p2_pnl A2B_sflow_p3_pnl A2B_sflow_p4_pnl
save touse_bootstrap.dta, replace

* Event time period
forvalues i=1(1)4 {
clear 
use touse_bootstrap.dta
gen Y`i'=A2B_sflow_p`i'_pnl
drop if Y`i'==.
save touse_bootstrap_t`i'.dta, replace
clear
postfile att_results_D`k'_t`i' att rep using att_results_D`k'_t`i', replace

	* Bootstrap repetitions: 1000
	forvalues h=1(1)1000 {
	* Generate random variable for random sampling
	use touse_bootstrap_t`i'.dta
	di "------Bootstrap repetition `h'-------"
	quietly generate random = runiform()
	quietly replace random=2 if D`k'==1
	quietly order random
	quietly gsort -random
	* Keep the treated and 1500 randomly sampled control disputes
	quietly keep in 1/1500
	quietly drop random

	* Matching
	*su Y`i' if D`k'==1
	*su Y`i' if D`k'==0
	nnmatch Y`i' D`k' `vars', tc(att) level(95) exact(`exact' `exact2') keep(attmatch_D`k'_p`i'_b`h') replace
 	matrix att=el(e(b),1,1)
	svmat att
	su att
	local att=r(mean)
	drop att
	post att_results_D`k'_t`i' (`att') (`h')
	clear
		
}
postclose att_results_D`k'_t`i'

erase touse_bootstrap_t`i'.dta
clear 
use att_results_D`k'_t`i'.dta

* 2. Get mean, lb and ub
sort att
gen aux=_n

drop if aux<25
drop if aux>975
su att

drop if rep>1
drop rep

replace att=r(mean)
gen ub=r(max)
gen lb=r(min)

gen t=`i'
save att_boots_D`k'_t`i'.dta, replace
erase att_results_D`k'_t`i'.dta
}

clear

* 3. Plot results

use att_boots_D1_t1.dta
append using att_boots_D1_t2.dta
append using att_boots_D1_t3.dta
append using att_boots_D1_t4.dta

save "Figure 2 Data.dta", replace

twoway  (rspike ub lb t) (scatter att t, msymbol(circle) mcolor(black) yline(0, lpattern(dash)) ylabel(#10, alternate labsize(small)) ytitle("Effect on Sectoral Trading Volume [Million US-$]") xtitle("Years after Ruling") legend(order(2 "Average Treatment Effect (Treated)" 1 "95% CI")))

graph export "Figure 2.pdf", replace
clear
exit
